<template>
    <view>
        <uni-segmented-control :current="menuIndex" :values="menuItems" style-type="text" active-color="#007aff" @clickItem="onClickItem" />

        <uni-list v-if="menuIndex === 0">
            <uni-list-item v-if="itemRankQuantity.length > 0" v-for="(item, index) in itemRankQuantity" :key="index">
                <template v-slot:header>
                    <uni-tag :text="index + 1" :type="getNo(index)" size="normal" />
                </template>
                <template v-slot:body>
                    <view style="flex: 1; margin-left: 10px">{{ item?.info?.giName }}</view>
                </template>
                <template v-slot:footer>
                    <view>{{ numberFormat(item.giButi, 0) }}</view>
                </template>
            </uni-list-item>
            <u-empty v-if="itemRankQuantity.length === 0" text="暂无数据"> </u-empty>
        </uni-list>

        <uni-list v-if="menuIndex === 1">
            <uni-list-item v-if="itemRankAmount.length > 0" v-for="(item, index) in itemRankAmount" :key="index">
                <template v-slot:header>
                    <uni-tag :text="index + 1" :type="getNo(index)" size="normal" />
                </template>
                <template v-slot:body>
                    <view style="flex: 1; margin-left: 10px">{{ item?.info?.giName }}</view>
                </template>
                <template v-slot:footer>
                    <view>¥{{ numberFormat(item.giPric, 2) }}</view>
                </template>
            </uni-list-item>
            <u-empty v-if="itemRankAmount.length === 0" text="暂无数据"> </u-empty>
        </uni-list>
    </view>
</template>

<script>
import { getItemRanks } from "../../utils/api";
import { mapGetters } from "vuex";
import {getNo} from "../../utils/helper.js"

export default {
    data() {
        return {
            menuItems: ["项目销量", "项目销售额"],
            menuIndex: 0,
            itemRankQuantity: [],
            itemRankAmount: [],
        };
    },

    onLoad() {
        this.loadItemRank({ gpUuid: this.activity.gpUuid, orderBy: "GI_BUTI_DESC" });
    },

    computed: {
        ...mapGetters({
            activity: "activityInfo/activity",
        }),
    },

    methods: {
        onClickItem(e) {
            if (this.menuIndex !== e.currentIndex) {
                this.menuIndex = e.currentIndex;
                if (e.currentIndex == 0) {
                    this.loadItemRank({ gpUuid: this.activity?.gpUuid, orderBy: "GI_BUTI_DESC" });
                }
                if (e.currentIndex == 1) {
                    this.loadItemRank({ gpUuid: this.activity?.gpUuid, orderBy: "GI_PRIC_DESC" });
                }
            }
        },
        async loadItemRank({ gpUuid, orderBy }) {
            let res = await getItemRanks({ gpUuid, orderBy });
            if (orderBy === "GI_BUTI_DESC") this.itemRankQuantity = res?.ranks?.filter((item) => item.giButi > 0) || [];
            if (orderBy === "GI_PRIC_DESC") this.itemRankAmount = res?.ranks?.filter((item) => item.giPric > 0) || [];
        },
    },
};
</script>

<style lang="scss" scoped></style>
